/**
 * 
 */
package com.yidu.drug.service.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.yidu.drug.common.Page;
import com.yidu.drug.common.Tools;
import com.yidu.drug.dao.DrugProductionModelMapper;
import com.yidu.drug.dao.ProductionEnterpriseModelMapper;
import com.yidu.drug.dao.ProductionModelMapper;
import com.yidu.drug.dao.QtSumModelMapper;
import com.yidu.drug.dao.QualityTestingModelMapper;
import com.yidu.drug.model.DrugProductionModel;
import com.yidu.drug.model.ProductionEnterpriseModel;
import com.yidu.drug.model.ProductionModel;
import com.yidu.drug.model.QtSumModel;
import com.yidu.drug.model.QualityTestingModel;
import com.yidu.drug.service.ProductionService;

/**
 * 生产计划的service实现类
 * @author 王志伟
 * @date 2018年8月27日 下午7:08:06
 * @version 1.0
 */
@Service
public class ProductionServiceImpl implements ProductionService{
	//生产计划的DAO
	@Resource
	private ProductionModelMapper proDao;
	
	//生产地址DAO
	@Resource
	private ProductionEnterpriseModelMapper preDao;
	
	//药品生产DAO
	@Resource
	private DrugProductionModelMapper dpDao;
	
	//质检总报表DAO
	@Resource
	private QtSumModelMapper qtSumDao;
	
	//质检DAO
	@Resource
	private QualityTestingModelMapper qtDao;
	
	@Override
	public int deleteByPrimaryKey(String proId,String preId) {
		dpDao.deleteByProId(proId);
		return proDao.deleteByPrimaryKey(proId);
	}

	@Override
	public int insert(ProductionModel record) {
		// TODO Auto-generated method stub
		return proDao.insert(record);
	}

	@Override
	public int insertSelective(ProductionModel record) {
		record.setProId(Tools.getRandomUUID());
		record.setIsva(Tools.ISVA_YES);
		record.setOperator("未制造");
		return proDao.insertSelective(record);
	}

	@Override
	public ProductionModel selectByPrimaryKey(String proId) {
		// TODO Auto-generated method stub
		return proDao.selectByPrimaryKey(proId);
	}

	@Override
	public int updateByPrimaryKeySelective(ProductionModel record) {
		// TODO Auto-generated method stub
		return proDao.updateByPrimaryKeySelective(record);
	}

	@Override
	public int updateByPrimaryKey(ProductionModel record) {
		// TODO Auto-generated method stub
		return proDao.updateByPrimaryKey(record);
	}

	@Override
	public Map<String, Object> findAll(String limit, String page, String entName,String operator) {
		//NEW一个生产的模型,加入页面信息和企业名称
		ProductionModel proModel = new ProductionModel();
		Page pa=new Page();
		pa.setPage(Integer.valueOf(page));
		pa.setRows(Integer.valueOf(limit));
		proModel.setPage(pa);
		proModel.setEntName(entName);
		proModel.setOperator(operator);
		//查询所有信息或者按条件查询
		List<ProductionModel> list=proDao.findAll(proModel);
		for (ProductionModel productionModel : list) {
			String preId=productionModel.getPreId();
			if(!Tools.isEmpty(preId)){
	        	ProductionEnterpriseModel pre=preDao.selectByPrimaryKey(preId);
	  	        System.out.println("地址名称"+pre.getPreSite());
	  	        productionModel.setArea(pre.getPreSite());
	        }
		}
		//查询出的信息的总行数
		Long count = proDao.findRows(proModel);
		System.out.println("总行数"+count);
		//加入MAP集合
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("code",0);
		map.put("msg","");
		map.put("count",count);
		map.put("data", list);
		return map;
	}

	
	@Override
	public int updateTjzj(String proId) {
		int row=0;
		Long sumNum=0L;
		List<DrugProductionModel> dpList=dpDao.findDrugIdByProId(proId);
		for (DrugProductionModel drugProductionModel : dpList) {
			sumNum=sumNum+drugProductionModel.getDrpNumber();
		}
		String SumSumnum=String.valueOf(sumNum);
		System.out.println("总数量为"+SumSumnum);
		QtSumModel qtSum=new QtSumModel();
		String qtSumId=Tools.getRandomUUID();
		qtSum.setSumId(qtSumId);
		qtSum.setSumSumnum(Integer.valueOf(SumSumnum));
		qtSum.setPoId(proId);
		qtSum.setIsva(Tools.ISVA_YES);
		qtSum.setFilId("1");
		qtSum.setSumNopass(0);
		qtSum.setSumRate(new BigDecimal(0));
		qtSum.setSumRemark("未质检");
		row=qtSumDao.insertSelective(qtSum);
		if(row>0){
			for (DrugProductionModel drugProductionModel : dpList) {
				QualityTestingModel qt=new QualityTestingModel();
				qt.setQtId(Tools.getRandomUUID());
				qt.setSumId(qtSumId);
				String num=String.valueOf(drugProductionModel.getDrpNumber());
				qt.setQtSumnum(Integer.valueOf(num));
				qt.setPwId(drugProductionModel.getDrugId());
				qt.setIsva(Tools.ISVA_YES);
				qt.setNopassNum(0);
				qt.setQtRate(new BigDecimal(0));
				//qt.setQtRemark("未质检");
				qtDao.insertSelective(qt);
			}
			ProductionModel  pro=new ProductionModel();
			pro.setProId(proId);
			pro.setSort(1);
			row=proDao.updateByPrimaryKeySelective(pro);
		}
		return row;
	}

}
